/*******MCircle.cpp******/

#include "MCircle.h"
#include <GL\glut.h>
static const float TWOPI = 2*3.1415926;

MCircle::MCircle(float ax, float ay, float ar, int an):x(ax),y(ay),r(ar),n(an)
{
	step = 1;
	speed = 0;
	if(ax<0 || ay<0 || ar<0 || an<=1) {//determine if the circle is valid
		points = NULL;
	}
	else {
		points = new pair<float,float>[n];
		for(int i = 0;i<n;i++) {
			float degree = i*TWOPI/n;
			points[i].first = sin(degree)*r+x;
			points[i].second = cos(degree)*r+y;
		}
	}
	name = "CIRCLE";
}


MCircle::~MCircle(void)
{
	delete []points;
}

void MCircle::drawShape() {
	if(points != NULL) {
		 glColor3f(cr,cg,cb);
		 if(step<n)
			 step++;
		 glBegin(GL_TRIANGLE_FAN);
		 for(int i = 0;i<step;i++) {
			 glVertex2f(points[i].first,points[i].second);
		 }
		 glEnd();
	}
}

void MCircle::fallDown(float t, list<Shape*> *list ) {
	if(bStop)	//if the circle on the plane
		return;
	float offset = speed*t+.5*Shape::acceleration*t*t;	//fall down distance d=vt+1/2at^2
	speed += Shape::acceleration*t;	//update speed
	if((y - offset) > r) { //not reaching the plane yet
		y -= offset;
	} else {	//reaching the plane
		offset = y-r;
		y = r;
		speed = 0;
		bStop = true;
	}
	for(int i = 0;i<n;i++) {
		points[i].second -= offset;
	}
	
	CollisionDetector::collisionDetect(this,list);
}

void MCircle::shift(float x1, float y1) {
	x += x1;
	y += y1;
	for(int i = 0;i<n;i++) {
		float degree = i*TWOPI/n;
		points[i].first = sin(degree)*r+x;
		points[i].second = cos(degree)*r+y;
	}
}


float MCircle::getMinX() const {
	return x-r;
}

float MCircle::getMaxX() const {
	return x+r;
}